package com.example.fish.service.admin;

import com.example.fish.mapper.*;
import com.example.fish.pojo.*;
import com.example.fish.service.CommentService;
import com.example.fish.service.SecretService;
import com.example.fish.service.admin.dto.BlogDTO;
import com.example.fish.service.admin.dto.SecretDTO;
import com.example.fish.service.admin.dto.UserInfoDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class AdminServiceImpl implements AdminService{
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BlogMapper blogMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private SecretMapper secretMapper;
    @Resource
    private IllegalMapper illegalMapper;
    @Resource
    private PromotionMapper promotionMapper;
    @Override
    public List<Integer> getTotal() {
//        总人数
        int i = userInfoMapper.countByExample(null);
//        男
        UserInfoExample example1= new UserInfoExample();
        example1.createCriteria().andSexEqualTo(1);
        int gen = userInfoMapper.countByExample(example1);
//        女
        UserInfoExample example2= new UserInfoExample();
        example2.createCriteria().andSexEqualTo(0);
        int wom = userInfoMapper.countByExample(example2);
        List<Integer> list = new ArrayList<>();
        list.add(i);
        list.add(gen);
        list.add(wom);

        return list;
    }

    /***
     * 先遍历所有的地址信息，将第一个“,”前面的作为key，
     * @return <Key,Value>
     */
    @Override
    public List<Map<String,String>> getProvence() {
        List<String> address =new ArrayList<>();
        List<UserInfo> userInfos = userInfoMapper.selectByExample(null);
        for (UserInfo userInfo : userInfos) {
            String address1 = userInfo.getAddress().split(",")[0];
            address.add(address1);
        }
//        统计数量
        Map<String,Integer> countMap = new HashMap<>();
        for (String s : address) {
            countMap.put(s,countMap.getOrDefault(s,0)+1);
        }
        List<Map<String,String>> list = new ArrayList<>();
//        所有的键值对
        Set<Map.Entry<String,Integer>> entrySet = countMap.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            Map<String,String> map = new HashMap<>();
            map.put("value",entry.getValue().toString());
            map.put("name",entry.getKey());
            list.add(map);
        }
        return list;
    }

    @Override
    public PageInfo<UserInfoDTO> getUsers(String username, Integer sex, String redDate, Integer pageNum) {
        Integer pageSize= 5;
        UserInfoExample example = new UserInfoExample();
        UserInfoExample.Criteria criteria = example.createCriteria();
        if (username != null) {
            criteria.andNameLike("%"+username+"%");
        }
        if (sex != null) {
            criteria.andSexEqualTo(sex);
        }
        if (redDate != null) {
            criteria.andRegdateEqualTo(redDate);
        }
        int i = userInfoMapper.countByExample(example);
        PageHelper.startPage(pageNum,pageSize);
        List<UserInfo> userInfos = userInfoMapper.selectByExample(example);
        List<UserInfoDTO> list = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(userInfo,userInfoDTO);
            User user = userMapper.selectByPrimaryKey(userInfo.getUserid());
            IllegalExample illegalExample = new IllegalExample();
            illegalExample.createCriteria().andUseridEqualTo(user.getUserid());
            int j = illegalMapper.countByExample(illegalExample);
            userInfoDTO.setIsDelete(user.getIsdelete());
            userInfoDTO.setCount(j);
            list.add(userInfoDTO);
        }
        PageInfo<UserInfoDTO> pageInfo = new PageInfo<>(list);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(i);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }

    /***
     * 调用该方法时，实现isDelete的切换
     */
    @Override
    public void deleteUser(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (user.getIsdelete() == 0){
            user.setIsdelete(1);
        }else {
            user.setIsdelete(0);
        }
        userMapper.updateByPrimaryKey(user);
    }



    @Override
    public PageInfo<BlogDTO> agreeRank(Integer pageNum) {
        Integer pageSize = 5;
        BlogExample blogExample=new BlogExample();
        blogExample.setOrderByClause("agree asc");
        int i = blogMapper.countByExample(blogExample);
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogs = blogMapper.selectByExample(blogExample);
        List<BlogDTO> list = new ArrayList<>();
        for (Blog blog : blogs) {
            BlogDTO blogDTO = new BlogDTO();
            BeanUtils.copyProperties(blog,blogDTO);
//            根据blogId查询评论条数
            CommentExample commentExample = new CommentExample();
            commentExample.createCriteria().andBlogidEqualTo(blog.getPostid());
            int j = commentMapper.countByExample(commentExample);
            blogDTO.setCount(j);
            list.add(blogDTO);
        }
        PageInfo<BlogDTO> pageInfo = new PageInfo<>(list);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(i);
        pageInfo.setPageNum(pageNum);
        return pageInfo;
    }
    @Override
    public PageInfo<BlogDTO> timeRank(Integer pageNum) {
        Integer pageSize = 5;
        BlogExample blogExample=new BlogExample();
        blogExample.setOrderByClause("blogtime desc");
        int i = blogMapper.countByExample(blogExample);
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogs = blogMapper.selectByExample(blogExample);
        List<BlogDTO> list = new ArrayList<>();
        for (Blog blog : blogs) {
            BlogDTO blogDTO = new BlogDTO();
            BeanUtils.copyProperties(blog,blogDTO);
            //            根据blogId查询评论条数
            CommentExample commentExample = new CommentExample();
            commentExample.createCriteria().andBlogidEqualTo(blog.getPostid());
            int j = commentMapper.countByExample(commentExample);
            blogDTO.setCount(j);
            list.add(blogDTO);
        }
        PageInfo<BlogDTO> pageInfo = new PageInfo<>(list);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(i);
        pageInfo.setPageNum(pageNum);
        return pageInfo;
    }

    @Override
    public PageInfo<BlogDTO> getBlogByTitle(String title, Integer pageNum) {
        Integer pageSize = 5;
        BlogExample example =new BlogExample();
        example.createCriteria().andTitleLike("%"+title+"%");
        int i = blogMapper.countByExample(example);
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogs = blogMapper.selectByExample(example);
        List<BlogDTO> list = new ArrayList<>();
        for (Blog blog : blogs) {
            BlogDTO blogDTO = new BlogDTO();
            BeanUtils.copyProperties(blog,blogDTO);
            //            根据blogId查询评论条数
            CommentExample commentExample = new CommentExample();
            commentExample.createCriteria().andBlogidEqualTo(blog.getPostid());
            int j = commentMapper.countByExample(commentExample);
            blogDTO.setCount(j);
            list.add(blogDTO);
        }
        PageInfo<BlogDTO> pageInfo = new PageInfo<>(list);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(i);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }

    @Override
    public void deleteBlog(Integer id) {
        blogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<SecretDTO> secretList(Integer pageNum) {
        Integer pageSize = 5;
        SecretExample secretExample = new SecretExample();
        int i = secretMapper.countByExample(secretExample);
        PageHelper.startPage(pageNum,pageSize);
        List<Secret> secrets = secretMapper.selectByExample(secretExample);
        List<SecretDTO> list = new ArrayList<>();
        for (Secret secret : secrets) {
            SecretDTO secretDTO = new SecretDTO();
            BeanUtils.copyProperties(secret,secretDTO);
//            根据id查询username
            User user = userMapper.selectByPrimaryKey(secret.getUserid());
            secretDTO.setUsername(user.getUsername());
            list.add(secretDTO);
        }
        PageInfo<SecretDTO> pageInfo = new PageInfo<>(list);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(i);
        pageInfo.setPageNum(pageNum);
        return pageInfo;
    }

    @Override
    public PageInfo<SecretDTO> secretListByType(String type, Integer pageNum) {
        Integer pageSize = 5;
        SecretExample secretExample = new SecretExample();
        secretExample.createCriteria().andTypeEqualTo(type);
        int i = secretMapper.countByExample(secretExample);
        PageHelper.startPage(pageNum,pageSize);
        List<Secret> secrets = secretMapper.selectByExample(secretExample);
        List<SecretDTO> list = new ArrayList<>();
        for (Secret secret : secrets) {
            SecretDTO secretDTO = new SecretDTO();
            BeanUtils.copyProperties(secret,secretDTO);
            User user = userMapper.selectByPrimaryKey(secret.getUserid());
            secretDTO.setUsername(user.getUsername());
            list.add(secretDTO);
        }
        PageInfo<SecretDTO> pageInfo = new PageInfo<>(list);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(i);
        pageInfo.setPageNum(pageNum);
        return pageInfo;
    }

    @Override
    public PageInfo<SecretDTO> secretListByKey(String key, Integer pageNum) {
        Integer pageSize = 5;
        SecretExample secretExample = new SecretExample();
        SecretExample.Criteria criteria1 = secretExample.createCriteria();
        criteria1.andTypeLike("%"+key+"%");
        SecretExample.Criteria criteria2 = secretExample.createCriteria();
        criteria2.andTitleLike("%"+key+"%");
        secretExample.or(criteria2);
//        secretExample.createCriteria().andTitleLike("%"+key+"%");
        int i = secretMapper.countByExample(secretExample);
        PageHelper.startPage(pageNum,pageSize);
        List<Secret> secrets = secretMapper.selectByExample(secretExample);
        List<SecretDTO> list = new ArrayList<>();
        for (Secret secret : secrets) {
            SecretDTO secretDTO = new SecretDTO();
            User user = userMapper.selectByPrimaryKey(secret.getUserid());
            BeanUtils.copyProperties(secret,secretDTO);
            secretDTO.setUsername(user.getUsername());
            list.add(secretDTO);
        }
        PageInfo<SecretDTO> pageInfo = new PageInfo<>(list);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(i);
        pageInfo.setPageSize(pageSize);
        return pageInfo;
    }

    @Override
    public void deleteSecret(Integer id) {
        secretMapper.deleteByPrimaryKey(id);
    }

    /***
     * 保存promotion
     */
    @Override
    public void savePromotion(String title, String content, String type) {
        Promotion promotion = new Promotion();
        promotion.setContent(content);
        promotion.setTitle(title);
        promotion.setType(type);
//        得到当前时间
        promotion.setTime(new Date());
        promotion.setProShow(0);
        promotionMapper.insert(promotion);
    }

    /***
     * 分页查询promotion
     */
    @Override
    public PageInfo<Promotion> getPromotion(Integer pageNum) {
        Integer pageSize  = 7;
        PromotionExample example = new PromotionExample();
        int i = promotionMapper.countByExample(example);
        PageHelper.startPage(pageNum,pageSize);
        List<Promotion> promotions = promotionMapper.selectByExampleWithBLOBs(example);
        PageInfo<Promotion> pageInfo = new PageInfo<>(promotions);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(i);
        pageInfo.setPageNum(pageNum);
        return pageInfo;
    }

    @Override
    public void updatePromotion(Integer id) {
        Promotion promotion = promotionMapper.selectByPrimaryKey(id);
        if (promotion.getProShow() == 1){
            promotion.setProShow(0);
        }else {
            promotion.setProShow(1);
        }
        promotionMapper.updateByPrimaryKey(promotion);
    }

    @Override
    public void deletePromotion(Integer id) {
        promotionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Promotion> getAllPromotion() {
        PromotionExample example = new PromotionExample();
        example.createCriteria().andProShowEqualTo(1);
        return promotionMapper.selectByExample(example);
    }

    @Override
    public Promotion getById(Integer id) {
        return promotionMapper.selectByPrimaryKey(id);
    }
}
